package com.choosefine.base.shixiaobao.common.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

/**
 * 简单Id生成器
 * 
 * @author 顾文伟
 * 
 */
public class IDCreatUtil {

	public static String getUUID() {
		String s = UUID.randomUUID().toString();
		// 去掉"-"符号
		// return
		s = s.substring(0, 8) + s.substring(9, 13) + s.substring(14, 18)
				+ s.substring(19, 23) + s.substring(24);
		return s;
	}

	public static String[] getUUID(int number) {
		if (number < 1) {
			return null;
		}
		String[] ss = new String[number];
		for (int i = 0; i < number; i++) {
			ss[i] = getUUID();
		}
		return ss;
	}

	/**
	 * 获取当前格式化日期
	 * 
	 * @return
	 */
	public static String dateFormat() {

		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		return sf.format(date);
	}

	public static String getRandomString(int length) { // length表示生成字符串的长度
		String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		Random random = new Random();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; i++) {
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}

	public static String getCode(String type) {
		return getCode(type,5);
	}

	public static String getCode(String type, int length) {
		String date = DateUtils.getNowTime().toString();
		String temp = date.substring(date.length()-length, date.length());
		String rs = getRandomString(length);
		StringBuffer a = new StringBuffer();
		a.append(type).append(temp).append(rs);
		return a.toString();
	}
	
}
